package ink.xiaobaibai.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import ink.xiaobaibai.annotation.InsidePublicResources;
import ink.xiaobaibai.common.AddDayStatus;
import ink.xiaobaibai.common.FreeClientUserStatus;
import ink.xiaobaibai.common.MyRole;
import ink.xiaobaibai.entity.*;
import ink.xiaobaibai.enumOxy.BuyOrderStatus;
import ink.xiaobaibai.enumOxy.MonthlyfeeOrderStatus;
import ink.xiaobaibai.response.ResponseFormat;
import ink.xiaobaibai.service.*;
import ink.xiaobaibai.units.MySecurity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @description: Oa流程通知控制层
 * @author: 小白白
 * @create: 2021-08-01
 **/

@RestController
@RequestMapping("/oa")
@Api(tags = "OA流程控制层")
@CrossOrigin
@InsidePublicResources
public class OaController {

    @Autowired
    private IMonthlyfeeOrderService monthlyfeeOrderService;

    @Autowired
    private IMonthlyRefundDepositService refundDepositService;

    @Autowired
    private IBuyOrderService buyOrderService;

    @Autowired
    private IFreeClientUserService freeClientUserService;

    @Autowired
    private IAdminUserService adminUserService;

    @Autowired
    private IFreeOzoneDeviceService freeOzoneDeviceService;

    @Autowired
    private IMonthlyAddDayService addDayService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private static final String OP_PRE = "oa:";

    /**
     * 包月订单+押金退款+月费增加+购买订单+酒店人员+仓库收发货
     */
    @GetMapping("/list")
    @ApiOperation("查看自己待处理的OA流程")
    @InsidePublicResources
    public ResponseFormat getList() {
        Integer userId = MySecurity.getUserId();
        List<String> list = new ArrayList<>();
        if (this.stringRedisTemplate.hasKey(OP_PRE + userId)) {
            return ResponseFormat.success(list);
        }
        this.stringRedisTemplate.opsForValue().set(OP_PRE + userId, "1", 6, TimeUnit.HOURS);
        if (this.buyOrder()) {
            list.add("有购买流程待处理");
        }
        if (this.freeClient()) {
            list.add("酒店业务待处理");
        }
        if (this.monthlyAddDay()) {
            list.add("包月加天业务待处理");
        }
        if (this.deposit1() || this.deposit2()) {
            list.add("月费押金待处理");
        }
        if (this.monthly()) {
            list.add("月费订单流程待处理");
        }
        return ResponseFormat.success(list);
    }

    private boolean buyOrder() {
        Integer userId = MySecurity.getUserId();
        String role = MySecurity.getRole();
        QueryWrapper<BuyOrder> q1 = new QueryWrapper<>();
        if (!MySecurity.isPower(MyRole.POWER_BUY)) {
            if (role.equals(MyRole.DISTRIBUTOR)) {
                q1.eq("admin_user_id", userId);
                q1.notIn("status", BuyOrderStatus.complete.getStatus());
            }
            if (role.equals(MyRole.AGENT)) {
                q1.eq("admin_dep_id", this.adminUserService.getDepId(userId));
                q1.eq("status", BuyOrderStatus.agent.getStatus());
            }
        } else {
            q1.eq("status", BuyOrderStatus.admin.getStatus());
        }
        q1.last("limit 1");
        return this.buyOrderService.count(q1) != 0;
    }

    private boolean freeClient() {
        String role = MySecurity.getRole();
        Integer userId = MySecurity.getUserId();
        QueryWrapper<FreeClientUser> masterQ = new QueryWrapper<>();
        if (!MySecurity.isPower(MyRole.POWER_CLIENT)) {
            if (role.equals(MyRole.DISTRIBUTOR)) {
                QueryWrapper<FreeOzoneDevice> q2 = new QueryWrapper<>();
                q2.select("id");
                q2.eq("admin_user_id", userId);
                List<Integer> ids = this.freeOzoneDeviceService.list(q2).stream().map(FreeOzoneDevice::getId).collect(Collectors.toList());
                masterQ.in("device_id", ids);
                masterQ.notIn("status", FreeClientUserStatus.OK.getStatus());
            } else if (role.equals(MyRole.AGENT)) {
                QueryWrapper<FreeOzoneDevice> q2 = new QueryWrapper<>();
                q2.select("id");
                q2.eq("dep_id", this.adminUserService.getDepId(userId));
                List<Integer> ids = this.freeOzoneDeviceService.list(q2).stream().map(FreeOzoneDevice::getId).collect(Collectors.toList());
                masterQ.in("device_id", ids);
                masterQ.eq("status", FreeClientUserStatus.AGENT.getStatus());
            }
        } else {
            masterQ.eq("status", FreeClientUserStatus.ADMIN.getStatus());
        }
        masterQ.last("limit 1");
        return this.freeClientUserService.count(masterQ) != 0;
    }

    private boolean monthlyAddDay() {
        String role = MySecurity.getRole();
        QueryWrapper<MonthlyAddDay> q1 = new QueryWrapper<>();
        if (!MySecurity.isPower(MyRole.POWER_MONTHLY)) {
            if (role.equals(MyRole.AGENT)) {
                q1.eq("status", AddDayStatus.AGENT.getStatus());
            }
        } else {
            q1.eq("status", AddDayStatus.AGENT.getStatus());
        }
        q1.last("limit 1");
        return this.addDayService.count(q1) != 0;
    }

    private boolean deposit1() {
        Integer userId = MySecurity.getUserId();
        QueryWrapper<MonthlyfeeOrder> q1 = new QueryWrapper<>();
        q1.eq("admin_user_id", userId);
        q1.eq("status", MonthlyfeeOrderStatus.APDEPOSIT.getStatus());
        q1.last("limit 1");
        return this.monthlyfeeOrderService.count(q1) != 0;
    }

    private boolean deposit2() {
        String role = MySecurity.getRole();
        if (MySecurity.isPower(MyRole.POWER_MONTHLY)) {
            QueryWrapper<MonthlyfeeOrder> q1 = new QueryWrapper<>();
            q1.eq("status", MonthlyfeeOrderStatus.APDEPOSIT.getStatus());
            return this.monthlyfeeOrderService.count(q1) != 0;
        } else {
            if (role.equals(MyRole.AGENT)) {
                Integer userId = MySecurity.getUserId();
                Integer depId = this.adminUserService.getDepId(userId);
                QueryWrapper<MonthlyfeeOrder> q1 = new QueryWrapper<>();
                q1.eq("status", MonthlyfeeOrderStatus.APDEPOSIT.getStatus());
                q1.eq("admin_dep_id", depId);
                return this.monthlyfeeOrderService.count(q1) != 0;
            }
        }
        return false;
    }

    private boolean monthly() {
        String role = MySecurity.getRole();
        QueryWrapper<MonthlyfeeOrder> q1 = new QueryWrapper<>();
        if (MySecurity.isPower(MyRole.POWER_MONTHLY)) {
            q1.eq("status", MonthlyfeeOrderStatus.ADMIN.getStatus());
        } else {
            if (role.equals(MyRole.DISTRIBUTOR)) {
                q1.eq("status", MonthlyfeeOrderStatus.UNALLOCATED);
            } else {
                q1.eq("status", MonthlyfeeOrderStatus.AGENT);
            }
        }
        return this.monthlyfeeOrderService.count(q1) != 0;
    }

}
